﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Gift.js.swfupload
{
    public partial class imgupload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Drawing.Image original_image = null;
            System.Drawing.Bitmap final_image = null;
            System.Drawing.Bitmap real_image = null;
            System.Drawing.Graphics graphic = null;
            System.Drawing.Graphics real_graphic = null;
            MemoryStream ms = null;
            string mode = "";
            try
            {

                // Get the data
                HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];
                mode = Request.Form["mode"];
                original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
                int width = original_image.Width;
                int height = original_image.Height;

                int target_width = 72;
                int target_height = 65;

                if (mode == "img1")
                {
                    target_width = 130;
                    target_height = 173;
                }
                else if (mode == "img2")
                {
                    target_width = 72;
                    target_height = 65;
                }
                int new_width, new_height;

                float target_ratio = (float)target_width / (float)target_height;
                float image_ratio = (float)width / (float)height;

                if (target_ratio > image_ratio)
                {
                    new_height = target_height;
                    new_width = (int)Math.Floor(image_ratio * (float)target_height);
                }
                else
                {
                    new_height = (int)Math.Floor((float)target_width / image_ratio);
                    new_width = target_width;
                }

                new_width = new_width > target_width ? target_width : new_width;
                new_height = new_height > target_height ? target_height : new_height;
                /*
                 新建一个bmp图片(就是由像素组成的图片Bitmap)，然后建立一个graphic来对图片进行操作
                 * 这个主要是填充
                 */
                real_image = new System.Drawing.Bitmap(width, height);  // 真实图片大小
                real_graphic = System.Drawing.Graphics.FromImage(real_image);
                real_graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                real_graphic.Clear(System.Drawing.Color.Transparent);
                real_graphic.DrawImage(original_image, 0, 0, width, height);

                final_image = new System.Drawing.Bitmap(target_width, target_height);//新建一个bmp图片 
                graphic = System.Drawing.Graphics.FromImage(final_image);//新建一个画板 
                graphic.Clear(System.Drawing.Color.Transparent);//清空一下画布 
                graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height));
                int paste_x = (target_width - new_width) / 2;
                int paste_y = (target_height - new_height) / 2;
                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//设置高质量,低速度呈现平滑程度 
                graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);//在指定位置画图 
                //经目标压缩后的图片
                ms = new MemoryStream();
                final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                //真实图片大小的图片
                MemoryStream real_ms = new MemoryStream();
                real_image.Save(real_ms, System.Drawing.Imaging.ImageFormat.Png);

                if (mode == "img1")
                {
                    Session["img1"] = ms;
                }
                else if (mode == "img2")
                {
                    Session["img2"] = real_ms;
                    MemoryStream mss = (MemoryStream)HttpContext.Current.Session["img2"];
                }
                Response.StatusCode = 200;
                Response.Write("data:image/png;base64," + Convert.ToBase64String(ms.ToArray()));
            }
            catch (Exception ex)
            {
                Response.StatusCode = 500;
                Response.Write("An error occured：" + ex.Message);
                Response.End();
            }
            finally
            {

                Response.End();
            }

        }
    }
}